﻿using System;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Phoenix.Xna.Framework.Xml;
using Phoenix.Xna.Framework.ContentPipeline;
using Phoenix.Xna.Framework.Serialization;

namespace Phoenix.Xna.Framework.Graphics
{
    public class ParameterBinding : ValueBinding
    {
        public Effect Effect { get; set; }
        [SerializableProperty("")]
        public string Parameter { get; set; }
        public bool IsDirty { get; set; }

        public ParameterBinding()
        {
            IsDirty = true;
        }

        public void SetParameter()
        {
            SetParameter(false);
        }

        public void SetParameter(bool forceSet)
        {
            EffectParameter p = Effect.Parameters[Parameter];

            if (p != null && (forceSet || IsDirty))
            {
                if (Value is bool)
                {
                    p.SetValue((bool)Value);
                }
                else if (Value is bool[])
                {
                    p.SetValue((bool[])Value);
                }
                else if (Value is float)
                {
                    p.SetValue((float)Value);
                }
                else if (Value is float[])
                {
                    p.SetValue((float[])Value);
                }
                else if (Value is int)
                {
                    p.SetValue((int)Value);
                }
                else if (Value is int[])
                {
                    p.SetValue((int[])Value);
                }
                else if (Value is Vector2)
                {
                    p.SetValue((Vector2)Value);
                }
                else if (Value is Vector2[])
                {
                    p.SetValue((Vector2[])Value);
                }
                else if (Value is Vector3)
                {
                    p.SetValue((Vector3)Value);
                }
                else if (Value is Vector3[])
                {
                    p.SetValue((Vector3[])Value);
                }
                else if (Value is Vector4)
                {
                    p.SetValue((Vector4)Value);
                }
                else if (Value is Vector4[])
                {
                    p.SetValue((Vector4[])Value);
                }
                else if (Value is Matrix)
                {
                    p.SetValue((Matrix)Value);
                }
                else if (Value is Matrix[])
                {
                    p.SetValue((Matrix[])Value);
                }
                else if (Value is Texture2D)
                {
                    p.SetValue((Texture2D)Value);
                }

                IsDirty = false;
            }
        }

        #region ValueBinding Members

        [SerializableProperty("")]
        public override string ID { get; set; }

        [SerializableProperty(null)]
        public override object Value
        {
            get { return base.Value; }
            set
            {
                base.Value = value;
                IsDirty = true;
            }
        }

        public override void LoadContent()
        {
            base.LoadContent();
            IsDirty = true;
            SetParameter();
        }

        #endregion
    }
}
